# 如果需要静态链接，则在find_package前添加：option(FT4222_USE_STATIC "Link ft4222 static library" ON)

include(FindPackageHandleStandardArgs)

# 允许用户指定路径
set(ft4222_ROOT "" CACHE PATH "Root directory of ft4222 installation")
if(NOT ft4222_ROOT)
    set(ft4222_ROOT "$ENV{ft4222_ROOT}")
endif()

# 搜索头文件
find_path(ft4222_INCLUDE_DIR
    NAMES libft4222.h #更改文件名：LibFT4222.h -> libft4222.h，跟Linux保持一致
    HINTS ${ft4222_ROOT} "${CMAKE_CURRENT_LIST_DIR}/LibFT4222"
    PATH_SUFFIXES inc
)

# 确定架构和库名称
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(_arch amd64)
    set(_libname LibFT4222-64)  # 实际文件名保持不变
else()
    set(_arch i386)
    set(_libname LibFT4222)     # 实际文件名保持不变
endif()

# 选择静态库或动态库
option(FT4222_USE_STATIC "Link ft4222 static library" OFF)
if(FT4222_USE_STATIC)
    # 静态库需要根据 VS 版本选择路径
    if(MSVC_VERSION GREATER_EQUAL 1900)  # VS2015+
        set(_libsubdir ucrt)
    else()
        set(_libsubdir vs2013)
    endif()
    set(_libdir lib/${_libsubdir})
else()
    set(_libdir dll)
endif()

# 搜索库文件
find_library(ft4222_LIBRARY
    NAMES ${_libname}
    HINTS ${ft4222_ROOT} "${CMAKE_CURRENT_LIST_DIR}/LibFT4222"
    PATH_SUFFIXES "${_libdir}/${_arch}"
)

# 验证变量
find_package_handle_standard_args(ft4222
    REQUIRED_VARS ft4222_INCLUDE_DIR ft4222_LIBRARY
)

# 创建导入目标
if(ft4222_FOUND AND NOT TARGET ft4222::ft4222)
    add_library(ft4222::ft4222 UNKNOWN IMPORTED)
    set_target_properties(ft4222::ft4222 PROPERTIES
        IMPORTED_LOCATION "${ft4222_LIBRARY}"
        INTERFACE_INCLUDE_DIRECTORIES "${ft4222_INCLUDE_DIR}"
    )

    # 如果是静态链接，添加宏定义 FT4222_STATIC
    if(FT4222_USE_STATIC)
        add_compile_options(/MT) # 如果使用静态链接则增加 /MT 标志
        target_compile_definitions(ft4222::ft4222 INTERFACE FT4222_STATIC)
    endif()
endif()
